<template>
	<view>
		<topDropdown :isSxShow="true" :isPaiXu="true" :dropdown1="dropdown1" :options1="options1" :optionsSx="sxList"
			:optionsPx="optionsPx" :isDuoXuan="true" searchLabel1="商机名称" searchPh1="请输入商机名称"
			@searchBoxEmitFun="searchBoxEmitFun" @dropDownFun1="dropDownFun1" @optionSxFun="optionSxFun"
			@optionPxFun="optionPxFun" :isKhShow="true"></topDropdown>
		<dataNull v-if="list.length == 0" src="/static/img/chahua/gjNull.png" title="暂无商机" title1="请添加或者更换搜索添加">
		</dataNull>
		<scroll-view v-else scroll-y="true" :style="{height: scrollHeight}" @scrolltolower="selectDataFun"
			refresher-enabled :refresher-threshold="200" :refresher-triggered="triggered" refresher-background="gray"
			@refresherrefresh="onRefresh" @refresherrestore="onRestore">
			<view v-for="(item, index) in list" :key="index" @click="cardClickFun(item, index)">
				<chanceCard :item="item" :isSelect="isSelect" :index="index"></chanceCard>
			</view>
			<getMore :isMore="isMore"></getMore>
		</scroll-view>
		<addBtn url="./add"></addBtn>
	</view>
</template>

<script>
	let that = '';
	import {
		apiChance
	} from '@/api/crm.js'
	import {
		shareDropDown,
		dropDownByUser,
		sjDatePxData,
		sjSxDropDownData
	} from '@/utils/dropdown.js'
	import {
		getDayByNumFun,formatDate
	} from '@/utils/date.js'
	import dataNull from '@/components/dataNull/dataNull.vue'
	import addBtn from '@/components/addBtn/addBtn.vue'
	import getMore from '@/components/getMore/getMore.vue'
	import chanceCard from '@/components/card/chanceCard.vue'
	import topDropdown from '@/components/topDropdown/topDropdown.vue'
	export default {
		components: {
			dataNull,
			addBtn,
			getMore,
			chanceCard,
			topDropdown
		},
		data() {
			return {
				dropdown1: '全部',
				options1: [],
				optionsPx: sjDatePxData,
				sxList: [],
				list: [],
				pageNum: 1,
				isMore: true,
				scrollHeight: '667px',
				triggered: false,
				isSelect: false,
				pageType: '',
				sortObj: {update_date: -1},
				optionsReq: {}, // 第一个下拉框请求参数
				sortReq: {}, // 第二个排序请求参数
				dateReq: [], // 日期相关请求参数
				sxReq: {}, // 筛选想请求参数
				matchObj: {},
				searchValue: '',
				clientIdArr: [],
				tabNoEqualArr: [], //标签页不等于数据
				depId: undefined
			}
		},
		onLoad(e) {
			that = this;
			
			//多条件筛选整理
			let cstDictSx = uni.getStorageSync('cstDictSx') || '';
			let khxdlist=[
				{title: '商机来源',field: 'source',current: '',arr: cstDictSx.source},
				{title: '商机阶段',field: 'salestage',current: '',arr: cstDictSx.salestage},
			];
			
			let sx=sjSxDropDownData.concat(khxdlist);
			
			that.sxList = sx;
			
			let obj = {};
			if (uni.$userInfo.isDepManager) {
				that.options1 = shareDropDown;
				that.depId = uni.$userInfo.depManager_Id;
			} else {
				that.options1 = shareDropDown;
				obj.fuZeRenId = uni.$userInfo._id;
			}
			if (e.customer_id) {
				that.clientIdArr = [e.customer_id]
			}
			// if (e.next_gjDate) {
			// 	let dateArr = getDayByNumFun(e.next_gjDate);
			// 	let dateObj = {
			// 		field: 'next_gjDate',
			// 		sTime: dateArr[1],
			// 		eTime: dateArr[0]
			// 	};
			// 	let arr = that.sxList;
			// 	arr[1].current = 0;
			// 	that.sxList = arr;
			// 	that.dateReq.push(dateObj)
			// } else if (e.chengJiao_date) {
			// 	let dateArr = getDayByNumFun(-3);
			// 	let dateObj = {
			// 		field: 'chengJiao_date',
			// 		sTime: dateArr[1],
			// 		eTime: dateArr[0]
			// 	};
			// 	let arr = that.sxList;
			// 	arr[0].current = 1;
			// 	that.sxList = arr;
			// 	that.dateReq.push(dateObj)
			// } else {
			// 	let arr = that.sxList;
			// 	arr[0].current = '';
			// 	arr[1].current = '';
			// 	that.sxList = arr;
			// }
			
			that.matchObj = obj;
			
			// that.optionsReq = JSON.stringify(obj);
			
			that.pageType = e.type ? e.type : '';
			if (e.type) {
				that.isSelect = true;
			}
			uni.getSystemInfo({
				success(res) {
					that.scrollHeight = res.windowHeight - 40 + 'px';
				}
			})
			that.selectDataFun();
			uni.$on('deleteCardFun', that.deleteCardFun);
			uni.$on('updateSjListByIndex', that.updateSjListByIndex)
			uni.$on('addSjItemInListFun', that.addSjItemInListFun)
		},
		onBackPress() {
			uni.$off('deleteCardFun', that.deleteCardFun);
			uni.$off('updateSjListByIndex', that.updateSjListByIndex)
			uni.$off('addSjItemInListFun', that.addSjItemInListFun);
		},
		methods: {
			// 查询商机
			selectDataFun: function() {
				if (!that.isMore) {
					return
				}
				uni.showLoading({
					title: '加载中...',
					mask: true
				})
				
				let matchObj = {
					openstatus: 1,
					keywords: that.searchValue,
					customer_id:that.clientIdArr.join(),//客户id
				}
				
				Object.assign(matchObj, that.matchObj,that.optionsReq, that.sxReq);
				
				let reqObj = {
					matchObj: matchObj,
					sortObj: that.sortReq,
					pageNum: that.pageNum,
				}

				let reqData = {
					action: 'get_list',
					params: reqObj
				}
				apiChance(reqData)
					.then(res => {
						this.triggered = false;
						let data = res.data.data;
						if (that.pageNum == 1) {
							that.list = [];
						}
						if (data.length == 20) {
							that.pageNum += 1;
							that.isMore = true;
						} else {
							that.isMore = false;
						}
						that.list = that.list.concat(data);
						setTimeout(() => {
							//延时关闭  加载中的 loading框
							uni.hideLoading()
						}, 888);
					})
			},
			// 卡片点击方法
			cardClickFun: function(item, index) {
				if (that.pageType == 'genjin') {
					uni.$emit('gjOrderBindFun', item)
					uni.navigateBack()
				} else if (that.pageType != '') {
					uni.$emit('shangjiBindFun', item)
					uni.navigateBack()
				}
			},
			// 下拉框回调函数
			dropDownFun1: function(e) {
				let optionsReq = {};
				let userInfo = uni.$userInfo;
				that.listtype = undefined;
				if (e.label == '全部') {
					optionsReq.listtype = 'selfson';
				} else if (e.label == '我负责的') {
					optionsReq.listtype = 'self';
				} else if (e.label == '下属负责') {
					optionsReq.listtype = 'son';
				} else if (e.label == '共享我的') {
					optionsReq.listtype = 'share';
				} 

				that.optionsReq = optionsReq;
				console.log('第一个下拉参数：')
				console.log(optionsReq)
				
				that.cxGetDataFun();
			},
			// 筛选框回调函数
			optionSxFun: function(e) {
				let arr = e.arr;
				that.clientIdArr = e.selectKhList;
				console.log(that.clientIdArr)
				
				let matchReq = [];
				let dateReq = [];
				for (var i = 0; i < arr.length; i++) {
					if (arr[i].current !== '' && !arr[i].isDate) {
						matchReq.push({
							field: arr[i].field,
							value: arr[i].arr[arr[i].current]
						})
					} else if (arr[i].current !== '' && arr[i].isDate) {
						dateReq.push({
							field: arr[i].field,
							value: formatDate(arr[i].sTime) + '-' + formatDate(arr[i].eTime)
						})
					}
				}
				let reqObj = {};
				// 动态生成请求对象
				for (var i = 0; i < matchReq.length; i++) {
					reqObj[matchReq[i].field] = matchReq[i].value
				}
				//时间参数处理
				for (var i = 0; i < dateReq.length; i++) {
					reqObj[dateReq[i].field] = dateReq[i].value
				}
				that.sxReq = reqObj;
				
				console.log('第三个多条件筛选参数')
				console.log(that.clientIdArr)
				
				that.cxGetDataFun();
			},
			// 排序筛选框回调函数
			optionPxFun: function(arr) {
				let sortReq = {
					orderField: ''
				}
				let direaction = '';
				for (var i = 0; i < arr.length; i++) {
					if (arr[i].current !== '') {
						sortReq = {}
						if (arr[i].field == 'create_time') {
							sortReq.orderDirection = arr[i].current == 1 ? 'asc' : 'desc'
						}
						if (arr[i].field == 'update_time') {
							sortReq.orderDirection = arr[i].current == 1 ? 'asc' : 'desc'
						}
						if (arr[i].field == 'link_time') {
							sortReq.orderDirection = arr[i].current == 1 ? 'asc' : 'desc'
						}
						if (arr[i].field == 'bill_date') {
							sortReq.orderDirection = arr[i].current == 1 ? 'asc' : 'desc'
						}
						sortReq.orderField = arr[i].field;
						break;
					}
				}
				that.sortReq = sortReq;
				
				console.log('第二个排序回调的参数')
				console.log(sortReq)
				that.cxGetDataFun();
			},
			// 搜索框回调方法
			searchBoxEmitFun: function(e) {
				that.searchValue = e.searchValue1;
				that.cxGetDataFun()
			},
			// 下拉刷新
			onRefresh: function() {
				if (that.triggered) return
				that.triggered = true;
				that.cxGetDataFun();
			},
			onRestore: function(e) {
				that.triggered = false; // 需要重置
			},
			// 重新获取数据
			cxGetDataFun: function() {
				that.pageNum = 1;
				that.isMore = true;
				that.selectDataFun();
			},
			deleteCardFun: function(e) {
				let arr = that.list;
				arr.splice(e.index, 1);
				that.list = arr;
			},
			// add页面新增数据
			addSjItemInListFun: function(e) {
				that.cxGetDataFun()
			},
			// 通过下标更新list数据
			updateSjListByIndex: function(e) {
				that.$set(that.list, parseInt(e.index), e.obj)
			},
		}
	}
</script>

<style>
	page {
		background-color: #F8F8F8;
	}
</style>
